package cn.comtom.ewbs.signature.common.config;

import java.util.ArrayList;
import java.util.List;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import cn.dev33.satoken.annotation.SaIgnore;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.builders.RequestParameterBuilder;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.service.ParameterType;
import springfox.documentation.service.RequestParameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.ApiSelectorBuilder;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

/**
 * Swagger配置
 * @author liuhy
 */
@Configuration
@EnableSwagger2
public class Swagger2Config {

    private String basePackage = "cn.comtom.ewbs.signature.controller";

    /**
     * 开放api信息
     * @return 响应结果
     */
    private ApiInfo openApiInfo() {
        return new ApiInfoBuilder().title("签名验签系统   OPEN RESTful API文档").description("开放接口参考和查看API详细信息")
                .termsOfServiceUrl("http://www.comtom.cn/")
                .contact(new Contact("liuhy", "", ""))
                .version("1.0").build();
    }


    /**
     * 认证api信息
     * @return 响应结果
     */
    private ApiInfo authApiInfo() {
        return new ApiInfoBuilder().title("签名验签系统   SAFE RESTful API文档").description("安全接口参考和查看API详细信息")
                .termsOfServiceUrl("http://www.comtom.cn/")
                .contact(new Contact("liuhy", "", ""))
                .version("2.0").build();
    }
    
    /**
     * 认证接口
     * @return 响应结果
     */
    @Bean
    public Docket createAuthApi(){
	   String[] paths = new String[]{"/auth/.*"};
	   StringBuilder pathRegex = new StringBuilder();
       for (String path : paths){
           pathRegex.append("(").append(path).append(")|");
       }
        ApiSelectorBuilder apiSelectorBuilder = new Docket(DocumentationType.SWAGGER_2).apiInfo(authApiInfo())
               .groupName("认证接口")
               .select()
               .apis(RequestHandlerSelectors.basePackage(basePackage)).paths(PathSelectors.regex(pathRegex.substring(0, pathRegex.length()-1)));
        RequestParameterBuilder tokenParam = new RequestParameterBuilder();
        RequestParameter requestParameter = tokenParam.name("token").description("访问令牌").required(false).parameterIndex(0).in(ParameterType.HEADER).build();
        List<RequestParameter> globalRequestParameters = new ArrayList<>();
        globalRequestParameters.add(requestParameter);
        return apiSelectorBuilder.build().globalRequestParameters(globalRequestParameters); 
   }
    
    /**
     * 开放接口
     * @return 响应结果
     */
    @Bean
    public Docket createOpenApi(){
	   String[] paths = new String[]{"/open/.*"};
	   StringBuilder pathRegex = new StringBuilder();
       for (String path : paths){
           pathRegex.append("(").append(path).append(")|");
       }
        ApiSelectorBuilder apiSelectorBuilder = new Docket(DocumentationType.SWAGGER_2).apiInfo(openApiInfo())
               .groupName("开放接口")
               .select()
               .apis(RequestHandlerSelectors.basePackage(basePackage).or(RequestHandlerSelectors.withMethodAnnotation(SaIgnore.class))).paths(PathSelectors.regex(pathRegex.substring(0, pathRegex.length()-1)));
        
        return apiSelectorBuilder.build();
   }

}
